//app.js
App({
  onLaunch: function () {
    let _this = this;
    if ( !wx.getStorageSync('token') ) {
      // 登录
      wx.login({
        success: res => {
          wx.request({
            url: this.globalData.serverUrl+'v1/api/user/oauth2',
            data:{code:res.code,state: 'xcx'},
            dataType: 'json',
            success:function(data){
              if ( data.data.code == 200 ) {
                wx.setStorageSync('token', data.data.data.token);
                if ( data.data.data.nickname && data.data.data.headimg_url ) {
                  wx.setStorageSync('userInfo', {nickname:data.data.data.nickname,headimg_url:data.data.data.headimg_url})
                }
                if (_this.tokenCallback) {
                  _this.tokenCallback(data.data.data.token)
                }
              }
            },
            fail: function (data) {
              wx.showToast({
                title: '服务器错误',
                icon: 'none',
                duration: 1500
              })
            },
            complete: function(data){
              if ( data.data.code != 200 ) {
                wx.showToast({
                  title: data.data.message,
                  icon: 'none',
                  duration: 1500
                })
              }
            }
          })
        }
      })
    }
  },
  globalData: {
    serverUrl: 'http://www.huachen.com/'
  }
})